
package com.hulk.tenant.parser;


import com.hulk.tenant.annotation.SqlParser;
import com.hulk.tenant.util.StringPool;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * SQL 解析辅助类
 *
 * @author hulk
 * @since 2018-07-22
 */
public class SqlParserHelper {

    public static final String DELEGATE_MAPPED_STATEMENT = "delegate.mappedStatement";

    /**
     * 获取 SqlParser 注解信息
     *
     * @param metaObject 元数据对象
     */
    public static boolean getSqlParserInfo(MetaObject metaObject) {
        String id = getMappedStatement(metaObject).getId();
        Class<?> clazz=null;
        //处理为类路径
        try {
            clazz=Class.forName(id.substring(0,id.lastIndexOf(".")));
        }catch (ClassNotFoundException e1){
            return false;
        }
        SqlParser sqlParser=clazz.getAnnotation(SqlParser.class);
        if(sqlParser!=null){
            return sqlParser.filter();
        }
        return false;
    }

    /**
     * 获取当前执行 MappedStatement
     *
     * @param metaObject 元对象
     */
    public static MappedStatement getMappedStatement(MetaObject metaObject) {
        return (MappedStatement) metaObject.getValue(DELEGATE_MAPPED_STATEMENT);
    }
}
